// MyFamily - XML based genealogy program.
// Copyright (C) 2006  Doug Swisher
// See the file COPYING.txt for license details.

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace DataModel
{
    public class GedComNode
    {
        private GedComLine line;
        private GedComNode parent;
        private GedComNode destination;
        private List<GedComNode> sources = new List<GedComNode>();
        private List<GedComNode> children = new List<GedComNode>();


        internal GedComNode()
        {
            this.line = GedComLine.CreateTop();
        }


        internal GedComNode(GedComNode parent, GedComReader reader)
        {
            this.parent = parent;
            this.line = reader.CurrentLine;
        }


        protected void Parse(GedComReader reader, Dictionary<string, GedComNode> xrefEntries)
        {
            if (!string.IsNullOrEmpty(this.Xref))
            {
                xrefEntries.Add(this.Xref, this);
            }

            while (!reader.Eof && (reader.CurrentLine.Level > this.line.Level))
            {
                GedComNode child = new GedComNode(this, reader);
                children.Add(child);
                reader.Advance();
                child.Parse(reader, xrefEntries);
            }
        }


        protected void SetXrefs(Dictionary<string, GedComNode> xrefEntries, GedComReadResults results)
        {
            if (!string.IsNullOrEmpty(this.Value) && this.Value.StartsWith("@"))
            {
                if (xrefEntries.ContainsKey(this.Value))
                {
                    // Source points to destination
                    GedComNode destination = xrefEntries[this.Value];
                    GedComNode source = this;

                    source.destination = destination;
                    destination.sources.Add(source);
                }
                else
                {
                    results.LogBadXref(this);
                }
            }

            foreach (GedComNode node in this.children)
            {
                node.SetXrefs(xrefEntries, results);
            }
        }


        protected void WriteDebugXmlMinion(XmlWriter writer)
        {
            writer.WriteStartElement(this.line.Tag);
            writer.WriteAttributeString("level", this.line.Level.ToString());

            if (!string.IsNullOrEmpty(this.line.Xref))
            {
                writer.WriteAttributeString("xref", this.line.Xref);
            }

            if (!string.IsNullOrEmpty(this.line.Value))
            {
                writer.WriteAttributeString("value", this.line.Value);
            }

            foreach (GedComNode child in this.children)
            {
                child.WriteDebugXmlMinion(writer);
            }

            writer.WriteEndElement();
        }


        public List<GedComNode> Children
        {
            get { return this.children; }
        }


        public int LineNumber
        {
            get { return this.line.LineNumber; }
        }


        public string Tag
        {
            get { return this.line.Tag; }
        }


        public string Value
        {
            get { return this.line.Value; }
        }


        public string Xref
        {
            get { return this.line.Xref; }
        }


        public GedComNode Destination
        {
            get { return this.destination; }
        }
    }
}
